package cc.fs.zhihuapp.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtils {
    public static SimpleDateFormat FORMAT_WITH_TIME = new SimpleDateFormat("yyyy年M月d日  HH:mm");//"yyyy-MM-dd HH:mm:ss z"
    public static SimpleDateFormat FORMAT_ONLY_DATE = new SimpleDateFormat("yyyy年M月d日");
    public static SimpleDateFormat FORMAT_ONLY_DATE2 = new SimpleDateFormat("yyyy-MM-dd");
    public static long ONE_MINUTE = 1000 * 60;
    public static long ONE_HOUR = ONE_MINUTE * 60;
    public static long ONE_DAY = ONE_HOUR * 24;
    public static long ONE_MONTH = ONE_DAY * 30;
    public static long ONE_YEAR = ONE_MONTH * 12;

    public static String difTimeJustNow = "刚刚";
    public static String difTimeMinutes = "分钟前";
    public static String difTimeHours = "小时前";
    public static String difTimeDays = "天前";
    public static String difTimeMonths = "月前";
    public static String difTimeYears = "年前";

    public static long TIME_UNLIMITED = 9999999999000l;

    public static String getNowDate() {
        Date dateNow = new Date();
        return FORMAT_WITH_TIME.format(dateNow);
    }

    public static String getDifTimeStr(long timeStamp) {
        String rlt = null;
        long dif = System.currentTimeMillis() - timeStamp;

        if (dif < (ONE_MINUTE * 2)) {
            rlt = difTimeJustNow;
        } else if (dif < ONE_HOUR) {
            rlt = String.valueOf(dif / ONE_MINUTE) + difTimeMinutes;
        } else if (dif < ONE_DAY) {
            rlt = String.valueOf(dif / ONE_HOUR) + difTimeHours;
        } else if (dif < ONE_MONTH) {
            rlt = String.valueOf(dif / ONE_DAY) + difTimeDays;
        } else if (dif < ONE_YEAR) {
            rlt = String.valueOf(dif / ONE_MONTH) + difTimeMonths;
        } else {
            rlt = String.valueOf(dif / ONE_YEAR) + difTimeYears;
        }

        return rlt;
    }

    public static String getAge(long birthdayTimeStamp) {
        String rlt = null;
        long dif = System.currentTimeMillis() - birthdayTimeStamp;

        rlt = String.valueOf(dif / ONE_YEAR);

        return rlt;
    }

    public static long parseWithTime(String timeStr) {
        try {
            return FORMAT_WITH_TIME.parse(timeStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String formatWithTime(long timeStamp) {
        try {
            return FORMAT_WITH_TIME.format(new Date(timeStamp));
        } catch (Exception e) {
            return "error";
        }
    }

    public static long parseOnlyDate(String birtyday) {
        try {
            return FORMAT_ONLY_DATE.parse(birtyday).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String formatOnlyDate(long timeStamp) {
        try {
            return FORMAT_ONLY_DATE.format(new Date(timeStamp));
        } catch (Exception e) {
            return "error";
        }
    }

    public static String formatOnlyDate2(long timeStamp) {
        try {
            return FORMAT_ONLY_DATE2.format(new Date(timeStamp));
        } catch (Exception e) {
            return "error";
        }
    }

    public static Date strToDate(String str, String tFormat) {
        try {
            return new SimpleDateFormat(tFormat).parse(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static String dateToStr(Date date, String tFormat) {
        try {
            String rlt = null;
            rlt = new SimpleDateFormat(tFormat).format(date);

            return rlt;
        } catch (Exception e) {
            return "error";
        }
    }

    public static String strFormat(String dateStr, String sFormat, String tFormat) {
        try {
            String rlt = null;
            Date date = new SimpleDateFormat(sFormat).parse(dateStr);
            rlt = new SimpleDateFormat(tFormat).format(date);

            return rlt;
        } catch (ParseException e) {
            return "error";
        }
    }


}
